package model.impl;

import java.util.LinkedList;
import java.util.List;

import model.EMenuState;
import model.impl.menu.CharacterSelectionState;
import model.impl.menu.ConnectingState;
import model.impl.menu.IMainMenu;
import model.impl.menu.IMenuState;
import model.impl.menu.MainMenuState;
import model.impl.menu.SettingsState;

import org.newdawn.slick.Input;

import controller.impl.KeyPress;


class MenuState implements IModelState, IMainMenu {
	private IMenuState currentState;
	private static final MenuState INSTANCE = new MenuState();
	private List<IMenuState> states = new LinkedList<IMenuState>();
	
	private MenuState() {
		states.add(MainMenuState.getInstance());
		states.add(SettingsState.getInstance());
		states.add(CharacterSelectionState.getInstance());
		states.add(ConnectingState.getInstance());
		currentState = MainMenuState.getInstance();
	}
	
	public static MenuState getInstance() {
		return INSTANCE;
	}
	
	@Override
	public void newInput(KeyPress keyPress) {
		int key = keyPress.getKey();
		char c = keyPress.getChar();
		
		if (key == Input.KEY_UP) {
			currentState.previousAlternative();
		} else if (key == Input.KEY_DOWN) {
			currentState.nextAlternative();
		} else if (key == Input.KEY_LEFT) {
			currentState.moveAlternativeLeft();
		} else if (key == Input.KEY_RIGHT) {
			currentState.moveAlternativeRight();
		} else if (key == Input.KEY_ENTER) {
			currentState.fireAlternative();
		} else if (key == Input.KEY_BACK) {
			currentState.removeCharacter();
		} else if (key == Input.KEY_PERIOD) {
			currentState.insertCharacter('.');
		} else if (Character.getNumericValue(c) != -1) {
			currentState.insertCharacter(c);
		}
	}
	
	@Override
	public void viewMainMenu() {
		currentState = MainMenuState.getInstance();
	}

	@Override
	public void viewSettings() {
		currentState = SettingsState.getInstance();
	}
	
	@Override
	public void viewCharacterSelection() {
		currentState = CharacterSelectionState.getInstance();
	}

	@Override
	public void addStateListener(StateListener listener) {
		for (IMenuState state : states) {
			state.addStateListener(listener);
		}
	}

	@Override
	public int getChosenCharacterIndex() {
		return currentState.getChosenCharacterIndex();
	}

	@Override
	public String getIP() {
		return currentState.getIP();
	}

	@Override
	public int getMarkedAlternative() {
		return currentState.getMarkedAlternative();
	}

	@Override
	public String getNick() {
		return currentState.getNick();
	}

	@Override
	public boolean isFullScreen() {
		return currentState.isFullScreen();
	}

	@Override
	public void viewConnecting() {
		currentState = ConnectingState.getInstance();
	}

	@Override
	public EMenuState getEMenuState() {
		return currentState.getEMenuState();
	}

	//This method is only supposed to be used in the in-gamestate
	@Override
	public void newData(Object obj) {
		
	}
	
	//This method is only supposed to be used in the in-gamestate
	@Override
	public void startGame() {
		
	}

	@Override
	public void reset() {
		for (IMenuState state : states) {
			state.resetMarkedAlternativePosition();
		}
	}
}
